/*
Copyright 2024 MySQL Parser Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"fmt"
	"log"

	"mysql-parser/sqlparser"
)

func main() {
	fmt.Println("=== 基础查询示例 ===")
	fmt.Println()

	// 基础SELECT查询示例
	basicQueries := []string{
		"SELECT * FROM users",
		"SELECT id, name FROM users",
		"SELECT name, email FROM users WHERE age > 18",
		"SELECT * FROM products ORDER BY price DESC",
		"SELECT * FROM orders LIMIT 10",
		"SELECT DISTINCT category FROM products",
		"SELECT name AS user_name, email AS user_email FROM users",
	}

	for i, sql := range basicQueries {
		fmt.Printf("示例 %d: %s\n", i+1, sql)
		
		// 解析SQL
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		// 提取表名
		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  涉及的表: %v\n", tables)

		// 分析表结构
		tableAnalysis := sqlparser.AnalyzeTables(stmt)
		fmt.Printf("  表分析: 表数量=%d, 有连接=%v, 有子查询=%v\n",
			tableAnalysis.TableCount, tableAnalysis.HasJoins, tableAnalysis.HasSubquery)

		// 查询复杂度分析
		complexity := sqlparser.AnalyzeQueryComplexity(stmt)
		fmt.Printf("  复杂度: %s (分数: %d)\n",
			complexity.ComplexityLevel, complexity.ComplexityScore)

		fmt.Println()
	}

	// INSERT示例
	fmt.Println("=== INSERT语句示例 ===")
	insertQueries := []string{
		"INSERT INTO users (name, email) VALUES ('John', 'john@example.com')",
		"INSERT INTO products (name, price, category) VALUES ('Laptop', 999.99, 'Electronics')",
		"INSERT INTO orders (user_id, product_id, quantity) VALUES (1, 2, 3)",
	}

	for i, sql := range insertQueries {
		fmt.Printf("INSERT示例 %d: %s\n", i+1, sql)
		
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  目标表: %v\n", tables)
		fmt.Println()
	}

	// UPDATE示例
	fmt.Println("=== UPDATE语句示例 ===")
	updateQueries := []string{
		"UPDATE users SET name = 'Jane' WHERE id = 1",
		"UPDATE products SET price = price * 0.9 WHERE category = 'Electronics'",
		"UPDATE orders SET status = 'shipped' WHERE created_at < '2024-01-01'",
	}

	for i, sql := range updateQueries {
		fmt.Printf("UPDATE示例 %d: %s\n", i+1, sql)
		
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  更新的表: %v\n", tables)
		fmt.Println()
	}

	// DELETE示例
	fmt.Println("=== DELETE语句示例 ===")
	deleteQueries := []string{
		"DELETE FROM users WHERE id = 1",
		"DELETE FROM products WHERE price < 10",
		"DELETE FROM orders WHERE status = 'cancelled'",
	}

	for i, sql := range deleteQueries {
		fmt.Printf("DELETE示例 %d: %s\n", i+1, sql)
		
		stmt, err := sqlparser.Parse(sql)
		if err != nil {
			log.Printf("解析错误: %v", err)
			continue
		}

		tables := sqlparser.ExtractAllTables(stmt)
		fmt.Printf("  删除的表: %v\n", tables)
		fmt.Println()
	}
}